import jieba
import re
import string
import langconv
import matplotlib
from matplotlib import pyplot as plt
import math
from pyecharts.charts import Geo
from pyecharts import options as opts
from pyecharts.globals import ChartType, SymbolType

jieba.setLogLevel(jieba.logging.INFO)

text_emo='week 3/weibo_half.txt'
stop_words='week 3/停用词库.txt'

def filter(lines): #数据过滤
    a=[]
    for i in lines:
        if i not in a:
            a.append(i)
    return a

def clean(text):#数据清洗
    text = re.sub(r"(回复)?(//)?\s*@\S*?\s*(:| |$)", " ", text)  # 去除正文中的@和回复/转发中的用户名
    #text = re.sub(r"\[\S+\]", "", text)      # 去除表情符号
    #text = re.sub(r"#\S+#", "", text)      # 保留话题内容
    URL_REGEX = re.compile(
        r'(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))',
        re.IGNORECASE)
    text = re.sub(URL_REGEX, "", text)       # 去除网址
    text = text.replace("转发微博", "")       # 去除无意义的词语
    text = re.sub(r"\s+", " ", text) # 合并正文中过多的空格
    text = re.sub('我在:',"",text)#去除我在：
    text = re.sub("我在这里:","",text)#去除我在这里：
    text=langconv.Converter('zh-hans').convert(text)#将繁体转换为简体
    return text.strip()

def seperate(txt):#分词
    after=[]
    pattern=re.compile(u'\t|\n|\.|-|:|;|\)|\(|\?|"')
    with open(stop_words,'r',encoding='UTF-8') as f:
        stop=list(f.read().split('\n'))
    for sen in txt:
        sen=re.sub(pattern,'',sen)
        b=jieba.lcut(sen,cut_all=False)
        after.append(b)
    def filter(lis):
        clean_all=[]
        clean_sen=[]
        for i in lis:
            for k in i:
                if k not in stop and len(k)>1:
                    clean_sen.append(k)
            clean_all.append(clean_sen)
            clean_sen=[]
        return clean_all
    return filter(after)

def emo_analysis(text):#情绪分析——选择第二种方法
    dic={'anger':0,'joy':0,'fear':0,'sadness':0,'disgust':0,'max':0}
    li=['week 3/anger.txt','week 3/joy.txt','week 3/fear.txt','week 3/sadness.txt','week 3/disgust.txt']
    li1=[]
    for j in li:
        with open(j,'r',encoding='UTF-8') as f:
                a=set(f.read().split('\n'))
                li1.append(a)
    li_=list(dic.keys())
    def emotions(text):
        num=0
        max=0
        for i in li1:
                for k in text:
                    if k in i:
                        dic[li_[num]]=dic[li_[num]]+1
                if dic[li_[num]]>max:
                    max=dic[li_[num]]
                    dic['max']=max
                num=num+1
        last=[k for k,v in dic.items()if v==dic['max']]
        if len(last)>2:
            return 'none'#无情绪输出none
        else:
            return last[0]
    return(emotions(text))

def emo_time(emo_list,time_list,time,emotion):
    month=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
    week=['Mon','Tue','Wed','Thu','Fri','Sat','Sun']
    hour=['{0:0>2d}'.format(i) for i in range(24)]
    model=['week','month','hour']
    model_li=[week,month,hour]
    ind=model.index(time)
    dic={}
    dic=dic.fromkeys(model_li[ind],0)
    num=0
    for i in time_list:
        if emo_list[num]==emotion:
            if ind==0:
                dic[i[0]]=dic[i[0]]+1
            elif ind==1:
                dic[i[1]]=dic[i[1]]+1
            else:
                dic[i[3][:2]]=dic[i[3][:2]]+1
        num=num+1
    value=list(dic.values())
    plt.plot(model_li[ind],value,"o-y")
    plt.title(emotion+'_'+time)
    plt.xlabel(emotion)
    plt.ylabel('times')
    #plt.text(model_li[ind],value,value,color='g',fontsize=10)
    plt.show()

def distance(city,loc):
    x1=float(loc[0])
    y1=float(loc[1])
    dis=math.sqrt((city[0]-x1)**2+(city[1]-y1)**2)
    return dis

def emo_loc(emo_list,loc_list,emotion,city):
    loc_li=['0-1','1-2','2-3','3-4','>4']
    loc_dic={}
    loc_dic=loc_dic.fromkeys(loc_li,0)
    total=0
    n=0
    for i in emo_list:
        if i==emotion:
            R=math.sqrt(distance(city,loc_list[n]))
            if R<0.5:
                loc_dic['0-1']=loc_dic['0-1']+1
            elif 0.5<=R<1:
                loc_dic['1-2']=loc_dic['1-2']+1
            elif 1<=R<1.5:
                loc_dic['2-3']=loc_dic['2-3']+1
            elif 1.5<=R<2:
                loc_dic['3-4']=loc_dic['3-4']+1
            else:
                loc_dic['>4']=loc_dic['>4']+1
            total=total+1
            n=n+1
    value=list(loc_dic.values())
    labels=list(loc_dic.keys())
    colors=['yellow','green','purple','blue','red']
    size=[i/total*100 for i in value ]
    plt.pie(size,labels=labels,colors=colors,autopct='%3.1f%%')
    plt.legend(loc='upper left',bbox_to_anchor=(-0.1,1))
    plt.axis('equal')
    plt.show()

def geo_emo(emo_list,loc_list):
    geo=Geo().add_schema(maptype='北京')
    emotion_type=['angry','sadness','joy','disgust','fear']
    for e in emotion_type:
        k=0
        data_pair=[]
        for i in emo_list:
            if i==e:
                geo.add_coordinate(e+str(k+1),loc_list[k][1],loc_list[k][0])
                data_pair.append((e+str(k+1),1))#?????
            k=k+1 
        geo.add(e,data_pair,symbol_size=5)
        geo.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
    geo.render()



if __name__ =='__main__': 
    txt=[]#文本
    loc=[]#地点
    time=[]#时间
    emo_ve=[]#情绪响亮
    flag=0
    with open(text_emo,'r',encoding='UTF-8') as f:
        lines=f.readlines()
    lines=filter(lines)
    for line in lines:
        if flag==1:
            a=line.split('\t')
            # if clean(a[1]) not in txt:
            txt.append(clean(a[1]))
            loc.append(a[0].lstrip('[').rstrip(']').split(','))
            time.append(a[3].strip('\n').split())
        else:
            flag=1
    txt=seperate(txt)
    for i in txt:
        emo_ve.append(emo_analysis(i))
    emo_time(emo_ve,time,'hour','sadness')
    #emo_loc(emo_ve,loc,'sadness',[39.7,116.2])
    #geo_emo(emo_ve,loc)
    


    
    

